package io.wanghaoran.project.module.system.controller;

import io.wanghaoran.project.framework.security.util.SecurityUtils;
import io.wanghaoran.project.module.system.entity.UserTokens;
import io.wanghaoran.project.module.system.service.UserTokensService;
import io.wanghaoran.project.common.base.domain.Result;
import io.wanghaoran.project.common.base.domain.TableDataInfo;
import io.wanghaoran.project.common.annotation.CrudLog;

import io.wanghaoran.project.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;

import org.springframework.stereotype.Controller;
import io.wanghaoran.project.common.base.controller.BaseController;

import javax.validation.Valid;
import java.util.Objects;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author authorname
 * @since 2025-07-30
 */
@Api(tags = "")
@Controller
@RequestMapping("/system/userTokens")
public class UserTokensController extends BaseController {

    private static final String SUFFIX = "system/userTokens";

    @Autowired
    private UserTokensService userTokensService;

    @GetMapping("/listPage")
    public String listPage() {
        return SUFFIX + "/list";
    }

    @ApiOperation(value = "分页列表", response = UserTokens.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页记录数", dataType = "Integer", paramType = "query")
    })
    @PreAuthorize("hasAuthority('userTokens:list')")
    @GetMapping(value = "/page")
    @ResponseBody
    public TableDataInfo page(UserTokens param) {

        startPage();

        if (!Objects.equals(SecurityUtils.getUserName(), "admin")) {
            param.setUserName(SecurityUtils.getUserName());
        }
        List<UserTokens> data = userTokensService.page(param);
        return getDataTable(data);
    }

    @GetMapping("/editPage/{id}")
    public String editPage(@Valid @NotNull(message = "id不能为空") @PathVariable Long id, ModelMap map) {
        Object data = userTokensService.info(id);
        map.put("userTokens", data);
        return SUFFIX + "/edit";
    }

    @GetMapping("/addPage")
    public String addPage() {
        return SUFFIX + "/add";
    }

    @CrudLog(title = "新增", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增")
    @PreAuthorize("hasAuthority('userTokens:add')")
    @PostMapping(value = "/add")
    @ResponseBody
    public Result add(@RequestBody @Validated UserTokens param, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.failure(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        userTokensService.add(param);
        return Result.success();
    }

    @CrudLog(title = "修改", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改")
    @PreAuthorize("hasAuthority('userTokens:edit')")
    @PostMapping(value = "/modify")
    @ResponseBody
    public Result modify(@RequestBody @Validated UserTokens param, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.failure(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        userTokensService.modify(param);
        return Result.success();
    }

    @CrudLog(title = "删除(单个条目)", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除(单个条目)")
    @PreAuthorize("hasAuthority('userTokens:del')")
    @DeleteMapping(value = "/remove/{id}")
    @ResponseBody
    public Result remove(@Valid @NotNull(message = "id不能为空") @PathVariable Long id) {
        userTokensService.remove(id);
        return Result.success();
    }

    @CrudLog(title = "删除(多个条目)", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除(多个条目)")
    @PreAuthorize("hasAuthority('userTokens:del')")
    @PostMapping(value = "/removes")
    @ResponseBody
    public Result removes(@RequestBody @Valid List<Long> ids) {
        userTokensService.removes(ids);
        return Result.success();
    }
}
